package com.jy.adscm.operation.mask.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.mask.bean.KzMaterial;
import com.jy.adscm.operation.mask.bean.KzMaterialDetail;
import com.jy.adscm.operation.mask.bean.QueryKzMaterial;
import com.jy.adscm.operation.mask.mapper.mysql.KzMaterialDetailMapper;
import com.jy.adscm.operation.mask.mapper.mysql.KzMaterialMapper;
import com.jy.adscm.operation.mask.service.KzMaterialDetailService;
import com.jy.adscm.operation.mask.service.KzMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-03-24
 */
@Service
public class KzMaterialServiceImpl extends ServiceImpl<KzMaterialMapper, KzMaterial> implements KzMaterialService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    CgSupplierMapper cgSupplierMapper;
    @Autowired
    KzMaterialDetailService kzMaterialDetailService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertKzMaterial(KzMaterial kzMaterial) {
         //查询当前的最大号
            boolean insert = super.insert(kzMaterial);
            return insert;
    }

    @Override
    public Page<KzMaterial> select(QueryKzMaterial queryKzMaterial) {
        Page<KzMaterial> page=new Page<>(queryKzMaterial.getCurrentPage(),queryKzMaterial.getPageSize());
        Wrapper<KzMaterial> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterial.getBatch(),null)){
            wrapper.like("batch",queryKzMaterial.getBatch());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterial.getCode(),null)){
            wrapper.like("code",queryKzMaterial.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterial.getStartTime(),null)){
            wrapper.ge("in_stock_date",queryKzMaterial.getStartTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryKzMaterial.getEndTime(),null)){
            wrapper.le("in_stock_date",queryKzMaterial.getEndTime());
        }
        wrapper.orderBy("create_time",false);
        page=super.selectPage(page,wrapper);
        List<KzMaterial> kzMaterials = page.getRecords();
        for (KzMaterial kzMaterial : kzMaterials) {
            loadKzMaterial(kzMaterial);
        }
        page.setTotal(selectCount(wrapper));

        return page;
    }

    @Override
    public KzMaterialDetail selectByCode(String code) {
        KzMaterialDetail result =new KzMaterialDetail();
         Wrapper<KzMaterialDetail> wrapper=new EntityWrapper<>();
         wrapper.eq("code",code);
            List<KzMaterialDetail> kzMaterialDetails = kzMaterialDetailService.selectList(wrapper);
            if (kzMaterialDetails!=null &&kzMaterialDetails.size()>0){
                result = kzMaterialDetails.get(0);
                Long pid = result.getPid();
                KzMaterial kzMaterial = super.selectById(pid);
                loadKzMaterial(kzMaterial);
                result.setKzMaterial(kzMaterial);
                if (AdscmBaseUtils.getInstance().isNotEmpty(result.getOutStocker(),null)){
                    JyUser jyUser = jyUserMapper.selectById(result.getOutStocker());
                    result.setOutStockerName(jyUser.getName());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(result.getCreator(),null)){
                    JyUser jyUser = jyUserMapper.selectById(result.getCreator());
                    result.setCreatorName(jyUser.getName());
                }
            }

        return result;
    }

    private void loadKzMaterial(KzMaterial kzMaterial) {
        if(AdscmBaseUtils.getInstance().isNotEmpty(kzMaterial.getMaterial(),null)){
            CgMaterial cgMaterial = cgMaterialMapper.selectById(kzMaterial.getMaterial());
            kzMaterial.setCgMaterial(cgMaterial);
        }
        if(AdscmBaseUtils.getInstance().isNotEmpty(kzMaterial.getSupplier(),null)){
            CgSupplier cgSupplier = cgSupplierMapper.selectById(kzMaterial.getSupplier());
            kzMaterial.setCgSupplier(cgSupplier);
        }
        if(AdscmBaseUtils.getInstance().isNotEmpty(kzMaterial.getInStocker(),null)){
            JyUser jyUser = jyUserMapper.selectById(kzMaterial.getInStocker());
            kzMaterial.setInStockerName(jyUser.getName());
        }
    }


}
